package com.weilan.blog.admin.controller;

import com.weilan.blog.admin.service.SysDictTypeService;
import com.weilan.blog.admin.vo.SysDictTypeVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.SysConf;

import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/9/28 15:33
 */
@Slf4j
@RestController
@RequestMapping("/sysDictType")
public class SysDictTypeController {
    @Autowired
    SysDictTypeService sysDictTypeService;

    /**
     * 获取字典类型列表
     *
     * @param sysDictTypeVO
     * @param result
     * @return
     */
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody SysDictTypeVO sysDictTypeVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取字典类型列表");
        return ResultUtil.result(SysConf.SUCCESS, sysDictTypeService.getPageList(sysDictTypeVO));
    }

    /**
     * 增加字典类型
     *
     * @param sysDictTypeVO
     * @param result
     * @return
     */
    @AvoidRepeatableCommit
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody SysDictTypeVO sysDictTypeVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysDictTypeService.addSysDictType(sysDictTypeVO);
    }

    /**
     * 编辑字典类型
     *
     * @param request
     * @param sysDictTypeVO
     * @param result
     * @return
     */
    @PostMapping("/edit")
    public String edit(HttpServletRequest request, @Validated({Update.class}) @RequestBody SysDictTypeVO sysDictTypeVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysDictTypeService.editSysDictType(sysDictTypeVO);
    }

    /**
     * 批量删除字典类型
     *
     * @param request
     * @param sysDictTypeVoList
     * @param result
     * @return
     */
    @PostMapping("/deleteBatch")
    public String delete(HttpServletRequest request, @Validated({Delete.class}) @RequestBody List<SysDictTypeVO> sysDictTypeVoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysDictTypeService.deleteBatchSysDictType(sysDictTypeVoList);
    }
}
